package com.maya.medicalwastes.dialog;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.maya.medicalwastes.R;
import com.maya.medicalwastes.base.LinePathView;
import com.maya.medicalwastes.util.DeviceUtil;
import com.maya.medicalwastes.util.FormatUtils;
import com.maya.medicalwastes.util.PermissionsChecker;

import java.io.IOException;


/**
 * *
 */
public class SureCollectDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private View view;
    private int heightRatio = 15;
    private int widthRatio = 7;

    private Button btnCancel;
    private Button btn_clear;
    private Button btnOk;
    private LinePathView mPathView;

    private boolean isCancelable = false;

    public SureCollectDialog(@NonNull Context context) {
        super(context, R.style.custom_dialog);
        this.context = context;
        initView();
        initDate();
    }

    private void initDate() {
    }

    private void initView() {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        view = layoutInflater.inflate(R.layout.dialog_collect, null);
        setContentView(view);
        btn_clear = findViewById(R.id.btn_clear);
        mPathView = findViewById(R.id.line);
        btnCancel = findViewById(R.id.btn_cancel);
        btnOk = findViewById(R.id.btn_ok);
        mPathView.setBackColor(Color.WHITE);
        mPathView.setPaintWidth(10);
        mPathView.setPenColor(Color.BLACK);
        btnOk.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll_close) {
            dismiss();
        }

        if (v.getId() == R.id.btn_ok) {
            if (onBtnClickListener != null) {
                String save = null;
                try {
                    save = mPathView.save(true, 10);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                onBtnClickListener.onOkClick(save,mPathView.getTouched());
                dismiss();

            }
        }

        if (v.getId() == R.id.btn_cancel) {
            if (onBtnClickListener != null) {
                onBtnClickListener.onCancelClick();
            }
            dismiss();
        }
        if (v.getId() == R.id.btn_clear) {
            mPathView.clear();
            mPathView.setBackColor(Color.WHITE);
            mPathView.setPaintWidth(10);
            mPathView.setPenColor(Color.BLACK);
        }
    }

    public void popDialog() {
        setCancelable(isCancelable);
        show();
        Window window = getWindow();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
        p.gravity = Gravity.CENTER;
//        p.height = (int) (DeviceUtil.getDisplayMetrics(context).heightPixels * 6 / heightRatio);
        p.width = DeviceUtil.getDisplayMetrics(context).widthPixels * 6 / widthRatio;
        window.setAttributes(p);
    }

    public OnBtnClickListener onBtnClickListener;

    public OnBtnClickListener getOnBtnClickListener() {
        return onBtnClickListener;
    }

    public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
        this.onBtnClickListener = onBtnClickListener;
    }

    public interface OnBtnClickListener {
        void onOkClick(String base,boolean flag);

        void onCancelClick();
    }

    public void setHeightRatio(int heightRatio) {
        this.heightRatio = heightRatio;
    }

    public void setWidthRatio(int widthRatio) {
        this.widthRatio = widthRatio;
    }

    public void setSizeRatio(int widthRatio, int heightRatio) {
        this.widthRatio = widthRatio;
        this.heightRatio = heightRatio;
    }

    @Override
    public void setCancelable(boolean cancelable) {
        isCancelable = cancelable;
    }
}
